<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:45
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
        $this->load->model('props/model_prop','prop');
        $propInfo = $this->prop->getAllLists();

        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getAllLists();

        $data = array(
            'propInfo'    =>   $propInfo,
            'fileInfo'      =>   $fileInfo
        );

        $this->load->view('admin/props/base/index',$data);
    }

    /*
     * 编辑道具
     * */
    public function create()
    {
        $pid = $this->uri->segment(4);

        $this->load->model('props/model_prop','prop');
        $propInfo = $this->prop->getById($pid);

        //图片信息
        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getById($propInfo['pic']);

        $data = array(
            'propInfo'  => $propInfo,
            'fileInfo'  => $fileInfo,
        );

        $this->load->view('admin/props/base/create',$data);

    }

    /*
     * 竞拍资料编辑后保存
     *
     * */
    public function save()
    {
        $pid = (int)$this->input->get_post('pid');

        $data = array
        (
            'name'                => trim($this->input->get_post('name')),
            //判断是否选择图片如果选择了图片则获取新突破地址或者判断是新建页面还是修改页面
            'pic'                 => !empty($_FILES['up_file']['tmp_name'])?$this->uploadFile('up_file'):(isset($pid)?$this->input->get_post('pic'):''),
            'amount'              => $this->input->get_post('amount'),
            'code'                => trim($this->input->get_post('code')),
            'view_type'           => (int)$this->input->get_post('view_type'),
            'discount_proportion' => $this->input->get_post('discount_proportion'),
            'sort'                => (int)$this->input->get_post('sort'),
        );


        //更新竞拍表
        $this->load->model('props/model_prop','prop');
        $this->prop->save($data, $pid);


        $this->load->helper('url');
        redirect(url('admin').'props/base/index');
    }

    /**
     * 删除道具
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $pid = (int)$this->input->get_post('pid');

            if (!$pid)
            {
                $response = error(140001);
                break;
            }

            $this->load->model('props/model_prop','prop');

            $res = $this->prop->delete(array('pid' => $pid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }

}